package constant.milk.periodapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import constant.milk.periodapp.R;
import constant.milk.periodapp.util.MilkUtil;

public class MilkCountDialog extends Dialog
{
	private TextView titleTextView;
	
	private TextView minusTextView;
	private TextView countTextView;
	private TextView plusTextView;
	
	private TextView noTextView;
	private TextView yesTextView;
	
	private String title;
	
	private CountCallBack callBack;
	private int count;
	
	private int max;
	private int min;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_count);
		
		setFindView();
		setOnClick();
		setData();
	}

	private void setFindView()
	{
		titleTextView = (TextView) findViewById(R.id.dialogCountTitleTextView);
		
		minusTextView = (TextView) findViewById(R.id.dialogCountMinusTextView);
		countTextView = (TextView) findViewById(R.id.dialogCountTextView);
		plusTextView = (TextView) findViewById(R.id.dialogCountPlusTextView);
		
		noTextView = (TextView) findViewById(R.id.dialogCountNoTextView);
		yesTextView = (TextView) findViewById(R.id.dialogCountYesTextView);
	}

	private void setOnClick()
	{
		minusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (count == min)
				{
					MilkUtil.showToast(getContext(), min + " 아래로 할 수 없습니다.");
					return;
				}
				
				count = count - 1;
				
				setData();
			}
		});
		
		plusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (count == max)
				{
					MilkUtil.showToast(getContext(), max + " 위로 할 수 없습니다.");
					return;
				}
				
				count = count + 1;
				
				setData();
			}
		});
		
		noTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
			}
		});
		
		yesTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				callBack.getCount(count);
				dismiss();
			}
		});
	}

	private void setData()
	{
		titleTextView.setText(title);
		
		countTextView.setText(count + "");
	}

	public MilkCountDialog(Context context, String title, CountCallBack callBack, int count)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		this.title = title;
		this.callBack = callBack;
		this.count = count;
		
		if (title.equals("생리주기"))
		{
			min = 10;
			max = 365;
		}
		else if (title.equals("생리기간"))
		{
			min = 1;
			max = 10;
		}
	}
	
	public interface CountCallBack
	{
		public abstract void getCount(int count);
	}
}
